﻿<%@ Page Language="C#" MasterPageFile="~/WebSite.Master" AutoEventWireup="true" CodeBehind="FunctionList.aspx.cs"
    Inherits="RN.ERP.Web.Manage.FunctionList" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="../Styles/skins/blue/css/style_msg.css" rel="stylesheet" type="text/css" />
    <link href="../Scripts/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../Scripts/jquery.min.js"></script>
    <script type="text/javascript" src="../Scripts/ligerUI/js/ligerui.min.js"></script>
    <script src="../Scripts/ligerUI/js/common.js" type="text/javascript"></script>
    <script src="../Scripts/ligerUI/js/LG.js" type="text/javascript"></script>
    <script type="text/javascript" src="../Scripts/ligerUI/js/plugins/ligerGrid.js"></script>
    <script type="text/javascript" src="../Scripts/ligerUI/js/plugins/ligerLayout.js"></script>
    <script src="../Scripts/ligerUI/js/ligerui.expand.js" type="text/javascript"></script>
    <script src="../Scripts/ligerUI/js/json2.js" type="text/javascript"></script>
    <script src="../Scripts/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
    <script type="text/javascript">
        var grid;
        var currentMenuParentNo;
        $(function () {
            var toolbarOptions = {
                items: [
            { text: '增加', click: add, img: "../images/add.png" },
            { line: true },
            { text: '修改', click: modify, img: "../images/application_edit.png" },
            { line: true },
            { text: '保存', click: f_save, img: "../images/page_save.png" },
            { line: true },
            { text: '取消', click: cancel, img: "../images/cancel.png" },
            { line: true },
            { text: '删除', click: deleterole, img: "../images/delete.png" },
            { line: true },
             { text: '映射', click: mapping, img: "../images/report.jpg" },
            { line: true }
        ]
            };

            var tree = $("#maintree").ligerTree({
                url: '../handlers/AuthorityHandler.ashx?act=getmenu',
                checkbox: false,
                nodeWidth: 180,
                onClick: function (node) {
                    if (!node.data.id) {
                        window.location.href = window.location.href;
                    }
                    var editingrow = grid.getEditingRow();
                    if (editingrow != null) {
                        grid.cancelEdit(editingrow);
                    }
                    currentMenuParentNo = node.data.id;
                    GetFunc(currentMenuParentNo);
                }
            });

            var layout = $("#layout").ligerLayout({ leftWidth: 200 });

            grid = $("#maingrid").ligerGrid({
                columns:
                [
                { display: '菜单名称', name: 'Title', type: "text", align: "left", editor: { type: 'text' }, width: 200 },
                { display: '编码', name: 'Code', type: "text", align: "left", editor: { type: 'text' }, width: 100 },
                { display: '地址', name: 'Url', type: "text", align: "left", editor: { type: 'text' }, width: 600 },
                { display: '描述', name: 'DESCRIPTION', type: "text", align: "left", editor: { type: 'text' }, width: 150 }
                ], dataAction: 'server', width: '98%', height: '98%', rowHeight: 20,
                columnWidth: 100, sortName: 'SortIndex', checkbox: false, rownumbers: true, pageSize: 20,
                toolbar: toolbarOptions, enabledEdit: true, clickToEdit: false, heightDiff: -5,
                data: []
            });

            grid.bind('beforeSubmitEdit', function (e) {
                var data = e.newdata;
                var isAddNew = e.record['__status'] == "add";
                if (!isAddNew)
                    data.FunctionGuid = e.record.FunctionGuid;
                else
                    data.FunctionGuid = "";
                if (data.Title == "" || data.Code == "" || data.Url == "") {
                    LG.tip('请完善信息!');
                    return false;
                }
                if (currentMenuParentNo) {
                    var count = 0;
                    for (var i = 0; i < grid.currentData.Total; i++) {
                        var obj = grid.filteredData.Rows[i];
                        if (obj.Title == data.Title && obj.Code == data.Code && obj.Url == data.Url && obj.FunctionGuid != data.FunctionGuid) {
                            count = count + 1;
                            break;
                        }
                    }
                    if (count > 0) {
                        LG.tip('数据已存在!');
                        return false;
                    }
                }
                return true;
            });

            grid.bind('afterSubmitEdit', function (e) {
                var isAddNew = e.record['__status'] == "add";
                var data = $.extend(true, {}, e.newdata);
                data.ParenGuid = currentMenuParentNo;
                if (!isAddNew)
                    data.FunctionGuid = e.record.FunctionGuid;
                $.ajax({
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    url: '../handlers/AuthorityHandler.ashx?act=addfunc&funcurl=' + escape(data.Url) + '&r=' + Math.random(),
                    data: data,
                    success: function (result) {
                        if (result.Message == "功能编号已存在") {
                            alert(result.Message);
                        }
                        else {
                            LG.tip(result.Message);
                        }
                        if (currentMenuParentNo) {
                            GetFunc(currentMenuParentNo);
                        } else {
                            window.location.href = window.location.href;
                        }
                    },
                    error: function () {
                        LG.tip('操作失败!');
                    }
                });
                return false;
            });
        });
        function add() {
            var editingrow = grid.getEditingRow();
            if (editingrow == null) {
                addNewRow();
            } else {
                LG.tip('请先保存或取消修改');
            }
        }

        function f_save() {
            var editingrow = grid.getEditingRow();
            if (editingrow != null) {
                grid.endEdit(editingrow);
            } else {
                LG.tip('现在不在编辑状态!');
            }
        }
        function modify() {
            var editingrow = grid.getEditingRow();
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            if (editingrow == null) {
                beginEdit();
            } else {
                LG.tip('请先保存或取消修改');
            }
        }
        function deleterole() {
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            jQuery.ligerDialog.confirm('确定删除吗?', function (confirm) {
                if (confirm)
                    f_delete();
            });
        }
        function cancel() {
            var editingrow = grid.getEditingRow();
            if (editingrow != null) {
                grid.cancelEdit(editingrow);
                grid.loadData();
            } else {
                LG.tip('现在不在编辑状态!');
            }
        }
        function f_delete() {
            var selected = grid.getSelected();
            if (!selected.FunctionGuid) {
                grid.deleteRow(selected);
                return;
            }
            if (selected) {
                $.ajax({
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    url: '../handlers/AuthorityHandler.ashx?act=deletefunc&funcid=' + selected.FunctionGuid + '&r=' + Math.random(),
                    success: function (result) {
                        LG.tip(result.Message);
                        if (currentMenuParentNo) {
                            GetFunc(currentMenuParentNo);
                        }
                        else {
                            grid.deleteRow(selected);
                        }
                    },
                    error: function () {
                        LG.showError('操作失败!');
                    }
                });
            }
            else {
                LG.tip('请选择行!');
            }
        }

        function mapping() {
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            if (!selected.FunctionGuid) {
                return;
            }
            else {
                $.ajax({
                    type: 'post',
                    cache: false,
                    dataType: 'json',
                    url: '../handlers/AuthorityHandler.ashx?act=addmapping&funccode=' + selected.Code + '&funcurl=' + selected.Url + '&r=' + Math.random(),
                    success: function (result) {
                        LG.tip(result.Message);
                    },
                    error: function () {
                        LG.showError('操作失败!');
                    }
                });
            }
        }

        function beginEdit() {
            var row = grid.getSelectedRow();
            if (!row) { LG.tip('请选择行'); return; }
            grid.beginEdit(row);
        }
        function addNewRow() {
            grid.addEditRow();
        }

        function GetFunc(data) {
            ////加载菜单
            $.ajax({
                type: 'get',
                cache: false,
                dataType: 'json',
                url: '../handlers/AuthorityHandler.ashx?act=getfunc&parent=' + data + '&r=' + Math.random(),
                success: function (result) {
                    grid.set('data', { Rows: result });
                }
            });
        }

    </script>
    <style type="text/css">
        .l-panel td.l-grid-row-cell-editing
        {
            padding-bottom: 2px;
            padding-top: 2px;
        }
        .l-body
        {
            width: 250px;
        }
        .l-layout-left
        {
            width: 200px;
            top: 0px;
            left: 0px;
            overflow: auto;
        }
        .l-layout-center
        {
            width: 1000px;
            top: 0px;
            left: 205px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="layout" style="width: 98%;">
        <div position="left" title="主菜单模块" id="mainmenu">
            <ul id="maintree">
            </ul>
        </div>
        <div position="center" title="子菜单列表">
            <div id="maingrid" style="margin: 2px;">
            </div>
        </div>
    </div>
    <ul class="iconlist">
    </ul>
</asp:Content>
